package com.yonyou.ucf.mdf.app.isvdxq.other.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.AccessTokenResponse;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.DcUrlResult;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.BaseService;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.MyTokenService;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;

/**
 * 获取网关地址示例
 * https://dbox.yonyoucloud.com/mdf-node/uniform/gdgy/api/gateway/getEnv?domainKey=p-mfc-dmm-p00s-isvplm
 * todo 载入应用套件suiteauth；
 */
@RestController
@RequestMapping("/gdgy/api/gateway")
public class GatewayController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GatewayController.class);

    @Autowired
    BaseService baseService;
    @Autowired
    private MyTokenService myTokenService;
    

    @GetMapping("/getGatewayAddress")
    @ResponseBody
    public DcUrlResult getGatewayAddress(@RequestParam(name = "tenantId") String tenantId) throws IOException {
        LOGGER.info("tenantId: {}", tenantId);
        DcUrlResult dcUrl = myTokenService.getGateway(tenantId);
        return dcUrl;
    }

    @RequestMapping("/getEnv")
    public String getEnv(HttpServletRequest request){
        String tenantId = baseService.getTenantId(request);
        HashMap<String, String> hashMap = new HashMap<>();
        String apiUrl = myTokenService.getProperty("runtime.server.url", null);
        String domainKey = myTokenService.getProperty("bpmrest.appsource", null);
        String env = myTokenService.getProperty("spring.profiles.active", null);
        AccessTokenResponse accessToken3 = myTokenService.getAccessToken3(tenantId);
        String mytoken = accessToken3.getAccessToken();
        hashMap.put("env", env);
        hashMap.put("domainKey", domainKey);
        hashMap.put("baseUrl", apiUrl);
        hashMap.put("token", mytoken);
        hashMap.put("apiUrl", String.format("%s%s", apiUrl, DxqConfig.pre_apiUrl));

        String result = JSONObject.toJSONString(hashMap);
        return result;
    }
}
